package org.example.review.controller;

import com.common.result.Result;
import lombok.RequiredArgsConstructor;
import org.example.review.dto.*;
import org.example.review.service.HrService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hr")
@RequiredArgsConstructor
public class hrController {
    private final HrService hrService; // 通过构造器注入

    /**
     * 获取所有用户信息列表
     * @return 包含用户列表的统一响应对象
     */
    @GetMapping("/list")
    public Result<List<UserDTO>> getHrList() {
        List<UserDTO> userList = hrService.gethrlist();
        // 直接返回成功结果，异常会被 GlobalExceptionHandler 自动处理
        return Result.success(userList);
    }
    @PostMapping("/add")
    public Result<String> addHr(@RequestBody UserDTO userDTO) {
        // 设置默认值
        if (userDTO.getCreateTime() == null) {
            userDTO.setCreateTime(java.time.LocalDateTime.now());
        }
        if (userDTO.getIsDeleted() == null) {
            userDTO.setIsDeleted(0);
        }
        hrService.addHr(userDTO);
        return Result.success("添加成功");
    }
    @DeleteMapping("/delete/{userId}")
    public Result<String> deleteHr(@PathVariable Integer userId) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(userId);
        hrService.deleteHr(userDTO);
        return Result.success("删除成功");
    }
    @PutMapping("/update/{userId}")
    public Result<String> updateHr(@PathVariable Integer userId, @RequestBody UserDTO userDTO) {
        userDTO.setUserId(userId);
        hrService.updateHr(userDTO);
        return Result.success("更新成功");
    }
    
    @PutMapping("/change/{userId}")
    public Result<String> changeHr(@PathVariable Integer userId, @RequestBody UserDTO userDTO) {
        userDTO.setUserId(userId);
        hrService.updateHr(userDTO);
        return Result.success("更新成功");
    }
    @PostMapping("/record")
    public Result<String> addRecord(@RequestBody HrRecordDTO hrRecord) {
        hrService.insertHrRecord(hrRecord);
        return Result.success("添加成功");
    }
    
    /**
     * 批量删除员工
     * @param request 包含要删除的用户ID列表
     * @return 统一响应结果
     */
    @DeleteMapping("/batch-delete")
    public Result<String> batchDeleteHr(@RequestBody BatchDeleteRequest request) {
        if (request.getIds() == null || request.getIds().isEmpty()) {
            return Result.error("用户ID列表不能为空");
        }
        int deletedCount = hrService.batchDeleteHr(request.getIds());
        return Result.success("成功删除 " + deletedCount + " 个用户");
    }
    
    /**
     * 批量更新员工信息
     * @param request 包含要更新的用户ID列表和更新数据
     * @return 统一响应结果，包含更新后的用户列表
     */
    @PutMapping("/batch-update")
    public Result<List<UserDTO>> batchUpdateHr(@RequestBody BatchUpdateRequest request) {
        if (request.getIds() == null || request.getIds().isEmpty()) {
            return Result.error("用户ID列表不能为空");
        }
        if (request.getUpdateData() == null) {
            return Result.error("更新数据不能为空");
        }
        
        // 将前端的updateData转换为UserDTO
        UserDTO userDTO = new UserDTO();
        BatchUpdateRequest.UpdateData updateData = request.getUpdateData();
        
        // 映射字段，只设置非空字段
        if (updateData.getDepartment() != null && !updateData.getDepartment().isEmpty()) {
            userDTO.setDepartment(updateData.getDepartment());
        }
        // 前端使用position，后端使用role
        if (updateData.getPosition() != null && !updateData.getPosition().isEmpty()) {
            userDTO.setRole(updateData.getPosition());
        }
        if (updateData.getRole() != null && !updateData.getRole().isEmpty()) {
            userDTO.setRole(updateData.getRole());
        }
        if (updateData.getAuthType() != null) {
            userDTO.setAuthType(updateData.getAuthType());
        }
        if (updateData.getEmail() != null && !updateData.getEmail().isEmpty()) {
            userDTO.setEmail(updateData.getEmail());
        }
        
        List<UserDTO> updatedUsers = hrService.batchUpdateHr(request.getIds(), userDTO);
        return Result.success(updatedUsers);
    }
    
    /**
     * 获取所有人事操作记录
     * @return 统一响应结果，包含操作记录列表
     */
    @GetMapping("/record/list")
    public Result<List<HrRecordDTO>> getHrRecordList() {
        List<HrRecordDTO> recordList = hrService.getHrRecordList();
        return Result.success(recordList);
    }
    
    /**
     * 删除单条操作记录
     * @param recordId 记录ID
     * @return 统一响应结果
     */
    @DeleteMapping("/record/delete/{recordId}")
    public Result<String> deleteHrRecord(@PathVariable Integer recordId) {
        int deletedCount = hrService.deleteHrRecordById(recordId);
        if (deletedCount > 0) {
            return Result.success("删除操作记录成功");
        } else {
            return Result.error("删除操作记录失败，记录不存在");
        }
    }
    
    /**
     * 批量删除操作记录
     * @param request 包含要删除的记录ID列表
     * @return 统一响应结果
     */
    @DeleteMapping("/record/batch-delete")
    public Result<String> batchDeleteHrRecords(@RequestBody BatchDeleteRecordsRequest request) {
        if (request.getRecordIds() == null || request.getRecordIds().isEmpty()) {
            return Result.error("记录ID列表不能为空");
        }
        int deletedCount = hrService.batchDeleteHrRecords(request.getRecordIds());
        return Result.success("成功删除 " + deletedCount + " 条操作记录");
    }
    
    /**
     * 更新操作记录的备注
     * @param recordId 记录ID
     * @param request 包含新备注的请求对象
     * @return 统一响应结果
     */
    @PutMapping("/record/update/{recordId}")
    public Result<String> updateHrRecordRemark(@PathVariable Integer recordId, @RequestBody UpdateRemarkRequest request) {
        if (request.getRemark() == null) {
            return Result.error("备注内容不能为空");
        }
        int updatedCount = hrService.updateHrRecordRemark(recordId, request.getRemark());
        if (updatedCount > 0) {
            return Result.success("更新备注成功");
        } else {
            return Result.error("更新备注失败，记录不存在");
        }
    }
}
